package top.hmtools.cssManager;

import java.io.IOException;
import java.io.PrintWriter;
import java.util.List;

import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpServletResponse;

import org.springframework.beans.factory.annotation.Autowired;

import top.hmtools.configurationManager.JsCssProperties;

/**
 * css相关操作controller
 * @author HyboJ
 * 创建日期：2017-9-27下午4:52:40
 */
public class CssController {
	
	@Autowired
	private ICssManager cssManager;
	
	@Autowired
	private JsCssProperties jsCssProperties;
	
	/**
	 * 获取合并的css文件内容
	 * @param request
	 * @param response
	 */
	public void getCss(HttpServletRequest request,HttpServletResponse response){
		PrintWriter writer = null;
		try {
			String cssNames = request.getParameter("cssNames");
			String cssContent = this.cssManager.getCss(cssNames);
			
			response.reset();
			response.setBufferSize(2048);
			response.setCharacterEncoding(this.jsCssProperties.getEncoding());
//			response.setContentLength(jsContent.length());
			response.setContentType("text/css");
			response.setStatus(HttpServletResponse.SC_OK);
			writer = response.getWriter();
			writer.print(cssContent);
			writer.flush();
		} catch (Exception e) {
			e.printStackTrace();
			try {
				response.sendError(HttpServletResponse.SC_INTERNAL_SERVER_ERROR,e.getMessage());
			} catch (IOException e1) {
				e1.printStackTrace();
			}
		}finally{
			if(writer != null){
				writer.close();
			}
		}
	}
	
	/**
	 * 刷新CSS缓存库
	 * @param request
	 * @param response
	 */
	public void refreshCss(HttpServletRequest request,HttpServletResponse response){
		PrintWriter writer = null;
		try {
			boolean refresh = this.cssManager.refresh();
			
			response.reset();
			writer = response.getWriter();
			writer.println("refresh css repertory status is "+refresh);
			writer.flush();
		} catch (Exception e) {
			e.printStackTrace();
			try {
				response.sendError(HttpServletResponse.SC_INTERNAL_SERVER_ERROR,e.getMessage());
			} catch (IOException e1) {
				e1.printStackTrace();
			}
		}finally{
			if(writer != null){
				writer.close();
			}
		}
	}
	
	/**
	 * 获取所有css缓存库中css文件名
	 * @param request
	 * @param response
	 */
	public void listCssMenu(HttpServletRequest request,HttpServletResponse response){
		PrintWriter writer = null;
		try {
			List<String> listCssFilenames = this.cssManager.listCssFilenames();
			
			response.reset();
			response.setBufferSize(2048);
			response.setCharacterEncoding(this.jsCssProperties.getEncoding());
//			response.setContentLength(jsContent.length());
			response.setContentType("text/html");
			response.setStatus(HttpServletResponse.SC_OK);
			writer = response.getWriter();
			writer.println("<h4>All css file name is:</h4>");
			writer.println("<a href=\""+this.jsCssProperties.getRefresh_css_uri()+"\"><h4>Refresh css store</h4></a>");
			for(String cssTmp:listCssFilenames){
				writer.println("<a href=\""+this.jsCssProperties.getCss_uri()+"?cssNames="+cssTmp+"\">"+cssTmp+"</a><br>");
				writer.flush();
			}
		} catch (Exception e) {
			e.printStackTrace();
			try {
				response.sendError(HttpServletResponse.SC_INTERNAL_SERVER_ERROR,e.getMessage());
			} catch (IOException e1) {
				e1.printStackTrace();
			}
		}finally{
			if(writer != null){
				writer.close();
			}
		}
	}

}
